package ods_map

import (
	"fmt"
	"server/global"
	"strings"
)


type DbFldMap struct {
	global.BaseModel
	SrcDbType string `json:"srcDbType" form:"srcDbType" gorm:"column:srcDbType;comment:源数据库类型;size:100;"`
	TgtDbType string `json:"tgtDbType" form:"tgtDbType" gorm:"column:tgtDbType;comment:目标数据库类型;size:100;"`

	SrcFldType string `json:"srcFldType" form:"srcFldType" gorm:"column:srcFldType;comment:源数据类型;size:100;"`
	TgtFldType string `json:"tgtFldType" form:"tgtFldType" gorm:"column:tgtFldType;comment:目标数据类型;size:100;"`

	SrcFldType1 string `json:"srcFldType1" form:"srcFldType1" gorm:"column:srcFldType1;comment:源数据类型附加信息;size:100;"`
	TgtFldType1 string `json:"tgtFldType1" form:"tgtFldType1" gorm:"column:tgtFldType1;comment:目标数据类型附加信息;size:100;"`

	SrcFldConv string `json:"srcFldConv" form:"srcFldConv" gorm:"column:srcFldConv;comment:字段转换方式;size:200;"`
}


func (m *DbFldMap) TableName() string {
	return "db_fld_Map"
}


func (m *DbFldMap) Save() error {
	err := global.SysDB.Save(&m).Error
	return err
}

func GetDbMap(srcDbType string, tgtDbType string) (map[string]string, error) {
	var dbFldMaps []DbFldMap
	err := global.SysDB.Where("srcDbType = ? and tgtDbType = ?", srcDbType, tgtDbType).Find(&dbFldMaps).Error
	if err != nil {
		return nil, err
	}
	dbTypeMap := make(map[string]string)
	if len(dbFldMaps) > 0 {
		for _, oneM := range dbFldMaps {
			dbTypeMap[strings.ToLower(oneM.SrcFldType)] = strings.ToLower(oneM.TgtFldType)
		}
		return dbTypeMap, nil
	} else {
		return nil, fmt.Errorf("未找到数据库映射关系")
	}
}
